#include "slm_cmd_json.h"
#include "at_slm332.h"
#include "cJSON.h"
#include "s_platform.h"
void json_get_cmd(char *cmd, char *replay, slm_cfg_t *slm_cfg_t);
void json_set_cmd(char *cmd, char *replay, slm_cfg_t *slm_cfg_t);

void slm_json_cmd_switch(char *cmd_t, char *replay_t)
{
    cJSON *json = cJSON_Parse(cmd_t);
    if (!json)
    {
        sprintf(replay_t, "Error parsing JSON");
        return;
    }

    // 解析 method
    cJSON *method = cJSON_GetObjectItem(json, "method");
    if (!method || !cJSON_IsString(method))
    {
        sprintf(replay_t, "Invalid method");
        cJSON_Delete(json);
        return;
    }
		
    if (strstr(method->valuestring, "get") != NULL)
    {
			
        json_get_cmd(cmd_t, replay_t, &slm_cfg);
    }
    else if (strstr(method->valuestring, "set") != NULL)
    {
			
        json_set_cmd(cmd_t, replay_t, &slm_cfg);
			
    }
    else if (strstr(method->valuestring, "other") != NULL)
    {
        // other cmd
        sprintf(replay_t, "Other command executed");
    }
    else
    {
        sprintf(replay_t, "Unknown method");
    }

    cJSON_Delete(json);
}
//提供设置MQTT 客户端的处理接口
extern uint8_t get_mg_mqttc_number(void);
extern void set_mg_mqtt_setup(uint8_t setup);
// 获取
void json_get_cmd(char *cmd, char *replay, slm_cfg_t *slm_cfg_t)
{
    cJSON *json = cJSON_Parse(cmd);
    if (!json)
    {
        return;
    }
		
    // 解析 cmd
    cJSON *pcmd = cJSON_GetObjectItem(json, "cmd");
    if (strstr(pcmd->valuestring, "mg_cfg_get") != NULL)
    {
			
        // get slm infmoation
        // 创建根对象
        cJSON *root = cJSON_CreateObject();
        // 创建 "arg" 对象
        cJSON *arg = cJSON_CreateArray();
        
        // 往根对象里面添加键值对method和cmd
        cJSON_AddStringToObject(root, "method", "get");
        cJSON_AddStringToObject(root, "cmd", "mg_cfg_get");
			  cJSON_AddItemToObject(root, "arg", arg);
        for (int i = 0; i < get_mg_mqttc_number(); i++) { // num_of_smqtt 是 smqtt 的数量
        cJSON *item = cJSON_CreateObject(); // 创建数组中的一个对象

        // 添加 smqtt 的信息到对象中
       cJSON_AddStringToObject(item, "ip_host", slm_cfg_t->smqtt[i].host);
       cJSON_AddNumberToObject(item, "port", slm_cfg_t->smqtt[i].port);
       cJSON_AddStringToObject(item, "clinet_id", slm_cfg_t->smqtt[i].client_id);
       cJSON_AddStringToObject(item, "user_name", slm_cfg_t->smqtt[i].user_name);
       cJSON_AddStringToObject(item, "pass_word", slm_cfg_t->smqtt[i].pass_word);
       cJSON_AddStringToObject(item, "subtopic", slm_cfg_t->smqtt[i].subtopic);
       cJSON_AddStringToObject(item, "pubtopic", slm_cfg_t->smqtt[i].pubtopic);
       cJSON_AddNumberToObject(item, "Qos", slm_cfg_t->smqtt[i].Qos);

        // 将对象添加到数组中
         cJSON_AddItemToArray(arg, item);
       }

        //*replay=cJSON_Print(root);
        cJSON_PrintPreallocated(root, replay, 1400, 0);
        cJSON_Delete(root);
				
    }
    else if (strstr(pcmd->valuestring, "mg_online_get") != NULL)
    {
        // get slm infmoation
        // 创建根对象
        cJSON *root = cJSON_CreateObject();
        // 创建 "arg" 对象
        cJSON *arg =  cJSON_CreateObject();
        // 往根对象里面添加键值对method和cmd
        cJSON_AddStringToObject(root, "method", "get");
        cJSON_AddStringToObject(root, "cmd", "mg_cfg_get");
   			cJSON_AddItemToObject(root, "arg", arg);
        // 往根arg对象里面添加键值对  slm的状态 信息
        cJSON_AddNumberToObject(arg, "slm_status",slm_cfg_t->slm_status);
        //*replay=cJSON_Print(root);
        cJSON_PrintPreallocated(root, replay, 1400, 0);
        cJSON_Delete(root);
    }
    else if(strstr(pcmd->valuestring, "mg_4g_get") != NULL)
    {
			//PRINT_FREE_HEAP_AROUND(
        cJSON *root = cJSON_CreateObject();
        // 创建 "arg" 对象
        cJSON *arg = cJSON_CreateObject();
       
        // 往根对象里面添加键值对method和cmd
        cJSON_AddStringToObject(root, "method", "get");
        cJSON_AddStringToObject(root, "cmd", "mg_4g_get"); 
		    cJSON_AddItemToObject(root, "arg", arg);
        // 往根arg对象里面添加键值对  slm的状态 信息
        cJSON_AddStringToObject(arg,"reg_ip",slm_cfg_t->slm332_net_info_t.slm_4g_ip);
        cJSON_AddStringToObject(arg,"iccid",slm_cfg_t->slm332_net_info_t.sim_iccid);
        cJSON_AddStringToObject(arg,"operator",slm_cfg_t->slm332_net_info_t.china_network_name);
        cJSON_AddNumberToObject(arg,"rssi",slm_cfg_t->slm332_net_info_t.db_singal);
        cJSON_AddStringToObject(arg,"imsi",slm_cfg_t->slm332_net_info_t.sim_imsi);
        cJSON_PrintPreallocated(root, replay, 1400, 0);
        cJSON_Delete(root);
			 // )
    }
		cJSON_Delete(json);
}
// 设置
void json_set_cmd(char *cmd, char *replay, slm_cfg_t *slm_cfg)
{
    cJSON *json = cJSON_Parse(cmd);
    if (!json)
    {
        return;
    }
    if (slm_cfg == NULL)
    {
        return;
    }
    // 解析 cmd  的arg
    cJSON *arg = cJSON_GetObjectItem(json, "arg");
		cJSON *index=cJSON_GetObjectItem(json,"index");
    if (!arg)
    {
        sprintf(replay, "arg not found in JSON");
        cJSON_Delete(json);
        return;
    }
    // 解析各个字段并设置到 slm_cfg
    cJSON *ip_host = cJSON_GetObjectItem(arg, "ip_host");
    cJSON *port = cJSON_GetObjectItem(arg, "port");
    cJSON *client_id = cJSON_GetObjectItem(arg, "clinet_id");
    cJSON *user_name = cJSON_GetObjectItem(arg, "user_name");
    cJSON *pass_word = cJSON_GetObjectItem(arg, "pass_word");
    cJSON *subtopic = cJSON_GetObjectItem(arg, "subtopic");
    cJSON *pubtopic = cJSON_GetObjectItem(arg, "pubtopic");
    cJSON *Qos = cJSON_GetObjectItem(arg, "Qos");
		uint8_t slm_pos=0;
		 if (index && cJSON_IsNumber(index))
    {
        slm_pos=index->valueint;
    }
		
    if (ip_host && cJSON_IsString(ip_host))
    {
        strncpy(slm_cfg->smqtt[slm_pos].host, ip_host->valuestring, sizeof(slm_cfg->smqtt[slm_pos].host) - 1);
    }
    if (port && cJSON_IsNumber(port))
    {
        slm_cfg->smqtt[slm_pos].port = port->valueint;
    }
    if (client_id && cJSON_IsString(client_id))
    {
        strncpy(slm_cfg->smqtt[slm_pos].client_id, client_id->valuestring, sizeof(slm_cfg->smqtt[slm_pos].client_id) - 1);
    }
    if (user_name && cJSON_IsString(user_name))
    {
        strncpy(slm_cfg->smqtt[slm_pos].user_name, user_name->valuestring, sizeof(slm_cfg->smqtt[slm_pos].user_name) - 1);
    }
    if (pass_word && cJSON_IsString(pass_word))
    {
        strncpy(slm_cfg->smqtt[slm_pos].pass_word, pass_word->valuestring, sizeof(slm_cfg->smqtt[slm_pos].pass_word) - 1);
    }
    if (subtopic && cJSON_IsString(subtopic))
    {
        strncpy(slm_cfg->smqtt[slm_pos].subtopic, subtopic->valuestring, sizeof(slm_cfg->smqtt[slm_pos].subtopic) - 1);
    }
    if (pubtopic && cJSON_IsString(pubtopic))
    {
        strncpy(slm_cfg->smqtt[slm_pos].pubtopic, pubtopic->valuestring, sizeof(slm_cfg->smqtt[slm_pos].pubtopic) - 1);
    }
    if (Qos && cJSON_IsNumber(Qos))
    {
        slm_cfg->smqtt[slm_pos].Qos = Qos->valueint;
    }
    cJSON_Delete(json);
    cJSON *root = cJSON_CreateObject();
    cJSON *parg = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "arg", parg);
    cJSON_AddStringToObject(root, "method", "set");
    cJSON_AddStringToObject(root, "cmd", "mg_cfg_set");

    // 往根arg对象里面添加键值对  slm的状态 信息
    cJSON_AddNumberToObject(parg, "set_status", 1);
    //*replay=cJSON_Print(root);
    cJSON_PrintPreallocated(root, replay, 1400, 0);
    cJSON_Delete(root);
}